#!/usr/bin/env python
#
# The Wayland backend's equivalent to the xephyr script
#
# The QTILE_XEPHYR environmental variable is set to 2 in this script, which can
# be used by configs to detect when they are run via this script.
#

import os
import signal
import subprocess
import sys
import time
from pathlib import Path

BASE_DIR = Path(__file__).parent.parent.resolve()
sys.path.insert(0, BASE_DIR)

from libqtile.utils import get_cache_dir, guess_terminal  # noqa: E402

CACHE_DIR = Path(get_cache_dir())
QTILE = BASE_DIR / "libqtile" / "scripts" / "main.py"


# This script can be configured with environmental variables and arguments:
outputs = os.environ.get("OUTPUTS", 1)
app = os.environ.get("APP", guess_terminal())
log_level = os.environ.get("LOG_LEVEL", "INFO")

cmd = [sys.executable, QTILE.as_posix(), "start", "-b", "wayland", "-l", log_level]
cmd.extend(sys.argv[1:])


# Find the display that the app needs
display = os.environ.get("WAYLAND_DISPLAY", "")
if display:
    display = display[:-1] + str(int(display[-1]) + 1)
else:
    display = "wayland-0"

os.environ["QTILE_XEPHYR"] = "2"
os.environ["WLR_X11_OUTPUTS"] = str(outputs)
os.environ["WLR_WL_OUTPUTS"] = str(outputs)

proc = subprocess.Popen(cmd)

time.sleep(1)
os.environ["WAYLAND_DISPLAY"] = display
app_proc = subprocess.Popen(app)


# Suppress KeyboardInterrupt messages
def noop(signal, frame):
    pass


signal.signal(signal.SIGINT, noop)

proc.wait()
